using B.S.RbacData.ErrorCode;
using ContentmanagementBaseWrite.API.Applications.Commands.Hospitals;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace ContentmanagementBaseWrite.API.Controllers
{
    /// <summary>
    /// 医院管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class HospitalController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<HospitalController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志记录器</param>
        public HospitalController(IMediator mediator, ILogger<HospitalController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }

        /// <summary>
        /// 新增医院
        /// </summary>
        /// <param name="command">新增医院命令</param>
        /// <returns>新增结果</returns>
        [HttpPost("add")]
        public async Task<ApiResult<int>> AddHospital([FromBody] AddNewHospitalCommand command)
        {
            try
            {
                _logger.LogInformation("接收到新增医院请求，医院名称：{HospitalName}", command.HospitalName);

                var result = await _mediator.Send(command);

                _logger.LogInformation("医院新增请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "医院新增请求处理异常");
                return new ApiResult<int>
                {
                    code = ResultEnumCode.fails,
                    message = $"医院新增请求处理异常：{ex.Message}",
                    data = 0
                };
            }
        }

        /// <summary>
        /// 更新医院
        /// </summary>
        /// <param name="command">更新医院命令</param>
        /// <returns>更新结果</returns>
        [HttpPut("update")]
        public async Task<ApiResult<int>> UpdateHospital([FromBody] UpdateHospitalCommand command)
        {
            try
            {
                _logger.LogInformation("接收到更新医院请求，医院ID：{Id}，医院名称：{HospitalName}", command.Id, command.HospitalName);

                var result = await _mediator.Send(command);

                _logger.LogInformation("医院更新请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "医院更新请求处理异常");
                return new ApiResult<int>
                {
                    code = ResultEnumCode.fails,
                    message = $"医院更新请求处理异常：{ex.Message}",
                    data = 0
                };
            }
        }

        /// <summary>
        /// 批量删除医院
        /// </summary>
        /// <param name="command">批量删除医院命令</param>
        /// <returns>删除结果</returns>
        [HttpPost("batch-delete")]
        public async Task<ApiResult<int>> BatchDeleteHospital([FromBody] BatchDeleteHospitalCommand command)
        {
            try
            {
                _logger.LogInformation("接收到批量删除医院请求，医院ID列表：{HospitalIds}", string.Join(",", command.HospitalIds));

                var result = await _mediator.Send(command);

                _logger.LogInformation("医院批量删除请求处理完成，结果：{Result}", result.message);

                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "医院批量删除请求处理异常");
                return new ApiResult<int>
                {
                    code = ResultEnumCode.fails,
                    message = $"医院批量删除请求处理异常：{ex.Message}",
                    data = 0
                };
            }
        }
    }
} 